//引入vue-router路由插件
import VueRouter from "vue-router";
//引入Vue
import Vue from "vue";
// 引入路由
import routes from "./routes";

//使用插件
Vue.use(VueRouter);

//重新封装 Router 中的 push/replace 方法,解决编程式路由多次点击后报错
let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace;
VueRouter.prototype.push = function (location, resolve, reject) {
  if (resolve && reject) {
    originPush.call(this, location, resolve, reject);
  } else {
    originPush.call(this,location,() => {},() => {});
  }
};
VueRouter.prototype.replace = function(location, resolve, reject) {
  if (resolve && reject) {
    originReplace.call(this, location, resolve, reject);
  } else {
    originReplace.call(this,location,() => {},() => {});
  }
};

//Router 实例
let router = new VueRouter({
  //工作状态,可以切换
  mode:'history', 
  //路由配置
  routes,
  
  //滚动行为
  scrollBehavior (to, from, savedPosition) {
    return { x: 0, y: 0 }
  }
});

//暴露 router 实例
export default router;